import allure
from playwright.sync_api import Page


class PageKeywords:
    def __init__(self, page: Page):
        self.page = page

    def go_to(self, url: str):
        """
        打开指定URL
        :param url: 要打开的URL
        """
        with allure.step(f"打开页面 {url}"):
            try:
                self.page.goto(url)
                print(f"PASS|成功打开 {url}")
            except Exception as e:
                print(f"FAIL|打开页面失败: {e}")
                raise e
                
    def refresh(self):
        """
        刷新当前页面
        """
        with allure.step("刷新页面"):
            try:
                self.page.reload()
                print("PASS|页面刷新成功")
            except Exception as e:
                print(f"FAIL|页面刷新失败: {e}")
                raise e
                
    def go_back(self):
        """
        返回上一页
        """
        with allure.step("返回上一页"):
            try:
                self.page.go_back()
                print("PASS|返回上一页成功")
            except Exception as e:
                print(f"FAIL|返回上一页失败: {e}")
                raise e
                
    def go_forward(self):
        """
        前进到下一页
        """
        with allure.step("前进到下一页"):
            try:
                self.page.go_forward()
                print("PASS|前进到下一页成功")
            except Exception as e:
                print(f"FAIL|前进到下一页失败: {e}")
                raise e
                
    def page_screenshot(self, path: str):
        """
        页面截图
        :param path: 截图保存路径
        """
        with allure.step(f"页面截图保存到 {path}"):
            try:
                self.page.screenshot(path=path)
                print(f"PASS|截图保存成功: {path}")
            except Exception as e:
                print(f"FAIL|截图保存失败: {e}")
                raise e
                
    def execute_script(self, script: str):
        """
        执行JavaScript脚本
        :param script: 要执行的JS脚本
        """
        with allure.step(f"执行JS脚本: {script}"):
            try:
                result = self.page.evaluate(script)
                print(f"PASS|JS脚本执行成功: {script}")
                return result
            except Exception as e:
                print(f"FAIL|JS脚本执行失败: {e}")
                raise e
                
    def get_url(self) -> str:
        """
        获取当前页面URL
        :return: 当前页面URL
        """
        with allure.step("获取当前页面URL"):
            try:
                url = self.page.url
                print(f"PASS|成功获取当前URL: {url}")
                return url
            except Exception as e:
                print(f"FAIL|获取URL失败: {e}")
                return None
                
    def new_tab(self, url: str = None) -> Page:
        """
        新建标签页
        :param url: 可选，新标签页要打开的URL
        :return: 新标签页的Page对象
        """
        with allure.step("新建标签页"):
            try:
                new_page = self.page.context.new_page()
                if url:
                    new_page.goto(url)
                print("PASS|成功新建标签页")
                return new_page
            except Exception as e:
                print(f"FAIL|新建标签页失败: {e}")
                raise e
                
    def switch_to_tab(self, index: int):
        """
        切换到指定索引的标签页
        :param index: 标签页索引(从0开始)
        """
        with allure.step(f"切换到第{index}个标签页"):
            try:
                pages = self.page.context.pages
                if index < len(pages):
                    self.page = pages[index]
                    print(f"PASS|成功切换到第{index}个标签页")
                else:
                    raise IndexError(f"标签页索引{index}超出范围")
            except Exception as e:
                print(f"FAIL|切换标签页失败: {e}")
                raise e
                
    def generate_pdf(self, path: str):
        """
        生成当前页面的PDF
        :param path: PDF保存路径
        """
        with allure.step(f"生成PDF保存到 {path}"):
            try:
                self.page.pdf(path=path)
                print(f"PASS|PDF生成成功: {path}")
            except Exception as e:
                print(f"FAIL|PDF生成失败: {e}")
                raise
                
    def get_html(self) -> str:
        """
        获取当前页面HTML内容
        :return: 页面HTML内容
        """
        with allure.step("获取页面HTML内容"):
            try:
                html = self.page.content()
                print("PASS|成功获取页面HTML内容")
                return html
            except Exception as e:
                print(f"FAIL|获取HTML失败: {e}")
                raise e
